package com.library.bookmanagement.util;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.models.auth.In;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：ZXM
 * @date ：Created in 2020/5/6 10:18
 * @description：统一返回结果类
 */
@JsonSerialize
public class ResultUtil<T> implements Serializable {
    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    // 返回码为100表示token超时
    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;


    @ApiModelProperty(value = "返回数据")
    private T data;

    // 私有构造参数
    private ResultUtil(){}

    // 成功静态方法
    public static ResultUtil ok(){
        ResultUtil resultUtil=new ResultUtil();
        resultUtil.setSuccess(true);
        resultUtil.setCode(ResultCode.SUCCESS);
        resultUtil.setMessage("成功");
        return resultUtil;
    }
    //失败静态方法
    public static ResultUtil error(){
        ResultUtil resultUtil=new ResultUtil();
        resultUtil.setSuccess(false);
        resultUtil.setCode(ResultCode.ERROR);
        resultUtil.setMessage("失败");
        return resultUtil;
    }
    // 形成链式编程代码-->返回this
    public ResultUtil success(Boolean success){
        this.setSuccess(success);
        return this;
    }

    public ResultUtil message(String message){
        this.setMessage(message);
        return this;
    }

    public ResultUtil code(Integer code){
        this.setCode(code);
        return this;
    }
    public ResultUtil data(T data){
        this.setData(data);
        return this;
    }
    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
